/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecom.beans.customer;

import ecom.beans.account.*;
import ecom.beans.address.*;


import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Tian
 */
@Stateless
public class CustomerEntityFacade implements CustomerEntityFacadeRemote {

    @PersistenceContext
    private EntityManager em;

    public CustomerEntity registerCustomer(
            String password,
            String email,
            String firstname, 
            String lastname,
            String tel,
            AddressEntity address,
            AccountEntity account
    ) {

        System.out.println("entree dans register customer");
        List<CustomerEntity> customerList = (List<CustomerEntity>) em.createNamedQuery("findCustomerByEmail").setParameter("email", email).getResultList();
        
        System.out.println("liste de customers creee");
        if (customerList.size() == 0) {

        	System.out.println("passage dans le if");
            CustomerEntity newCustomer = new CustomerEntity();

            newCustomer.setPassword(password);
            newCustomer.setEmail(email);
           
            newCustomer.setFirstname(firstname);
            newCustomer.setLastname(lastname);
            newCustomer.setTel(tel);
            newCustomer.setCustomerAddress(address);
            newCustomer.setCustomerAccount(account);
            
            em.persist(newCustomer);
            System.out.println("persistence effectuee");

            return newCustomer;

        } else {
            System.out.println("passage dans le else");
            return null;
        }
    }

	
	
	
	
    public boolean findCustomerEmail(String email) {

        CustomerEntity temp;
        try {
            temp = (CustomerEntity) em.createNamedQuery("findCustomerByEmail").setParameter("email", email).getSingleResult();

        } catch (Exception e) {

            return false;
        }
        if (temp == null) {
            return false;
        }
        return true;
    }

    public boolean findCustomerPassword(String password) {

        CustomerEntity temp;

        try {
            temp = (CustomerEntity) em.createNamedQuery("findCustomerByPassword").setParameter("password", password).getSingleResult();


        } catch (Exception e) {

            return false;
        }

        if (temp == null) {
            return false;
        }

        return true;
    }

    public CustomerEntity loginCustomer(String email, String password) {

        CustomerEntity temp;

        try {

            temp = (CustomerEntity) em.createNamedQuery("identifyClient").setParameter("email", email).setParameter("password", password).getSingleResult();

        } catch (Exception e) {

            temp = null;
        }

        return temp;
    }

    public List<CustomerEntity> getAllCustomers() {

        Query temp;

        try {

            temp = em.createNamedQuery("findAllCustomers");


        } catch (Exception e) {

            temp = null;
        }

        return (List<CustomerEntity>) temp.getResultList();

    }

    public List<CustomerEntity> removeCustomer(String email, String password) {

        List<CustomerEntity> temp;

        try {

            temp = (List<CustomerEntity>) em.createNamedQuery("identifyClient").setParameter("email", email).setParameter("password", password).getResultList();

            for (int i = 0; i < temp.size(); i++) {
                em.remove(temp.get(i));
            }
        } catch (Exception e) {

            temp = null;
        }

        return temp;
    }

    public List<CustomerEntity> removeCustomerById(Long id) {

        List<CustomerEntity> temp;

        try {

            temp = (List<CustomerEntity>) em.createNamedQuery("findCustomerById").setParameter("id", id).getResultList();

            for (int i = 0; i < temp.size(); i++) {
                em.remove(temp.get(i));
            }
        } catch (Exception e) {

            temp = null;
        }

        return temp;
    }

    public List<CustomerEntity> removeAllCustomers() {

        List<CustomerEntity> temp;

        try {

            temp = (List<CustomerEntity>) em.createNamedQuery("findAllCustomers").getResultList();

            for (int i = 0; i < temp.size(); i++) {
                em.remove(temp.get(i));
            }
        } catch (Exception e) {

            temp = null;
        }

        return temp;
    }
    
    public void updateCustomer(CustomerEntity customer){
        
         em.merge(customer);

    }
  
     public CustomerEntity registerCustomer(
            String password,
            String email,
            String firstname, 
            String lastname,
            String tel,
            int addressType,
            int streetNum,
            String streetName,
            int buildingNum,
            int floorNum,
            int apartmentNum,
            String zipcode,
            String city
    ) {
        List<CustomerEntity> customerList = (List<CustomerEntity>) em.createNamedQuery("findCustomerByEmail").setParameter("email", email).getResultList();
        
        if (customerList.size() == 0) {

            CustomerEntity newCustomer = new CustomerEntity();

            newCustomer.setPassword(password);
            newCustomer.setEmail(email);
           
            newCustomer.setFirstname(firstname);
            newCustomer.setLastname(lastname);
            newCustomer.setTel(tel);
            
            
            AddressEntity address = new AddressEntity();
            
            address.setAddressType(addressType);
            address.setApartmentNum(apartmentNum);
            address.setBuildingNum(buildingNum);
            address.setFloorNum(floorNum);
            address.setCity(city);
            address.setStreetNum(streetNum);
            address.setStreetName(streetName);
            address.setZipcode(zipcode);
                    
                    
            newCustomer.setCustomerAddress(address);
            
            em.persist(newCustomer);
           
            return newCustomer;

        } else {
           return null;
        }
    }
	
	public CustomerEntity findCustomerById(Long id) {

        CustomerEntity temp;

        try {

            temp = (CustomerEntity) em.createNamedQuery("findCustomerById").setParameter("id", id).getSingleResult();

        } catch (Exception e) {

            temp = null;
        }

        return temp;
    }

	/**
	 *@author Houssem Z. 
	 */
	public void persistCustomer(CustomerEntity customer){
		
		em.persist(customer);
	}
	
	
	
	
}
